<template>
  <div class="wrap">
    <div class="main">
      <div class="item">
        <div class="left" @click="my()">头像</div>
        <div class="right">
          <img src="@/assets/rightArrow.png" alt="" class="rightArrow" />
          <van-uploader
            v-model="fileList"
            :after-read="afterRead"
            :max-count="1"
            v-if="!resUserInfo.headImage"
          >
            <!-- <img
              src="@/assets/dele/forum1.png"
              alt=""
              class="head"
              
            /> -->
          </van-uploader>
          <div class="headImgWrap" v-if="resUserInfo.headImage">
            <img
              :src="$imgUrl + resUserInfo.headImage"
              alt=""
              class="head"
              @click="look($imgUrl + resUserInfo.headImage)"
            />
            <img src="@/assets/eidtor2.png" alt="" class="deleImg" @click="deleMyHead"/>
          </div>
        </div>
      </div>

      <div class="item">
        <div class="lineItem">
          <div class="leftLineItem">ID</div>
          <div class="rightLineItem">
            <em>{{ resUserInfo.id }}</em>
          </div>
        </div>
        <div class="lineItem">
          <div class="leftLineItem">手机号</div>
          <div class="rightLineItem">
            <em>{{ resUserInfo.mobile }}</em>
          </div>
        </div>
        <div class="lineItem" @click="showNickName = true">
          <div class="leftLineItem">昵称</div>
          <div class="rightLineItem">
            <em v-if="!resUserInfo.nickName">请设置昵称</em>
            <em v-else>{{ resUserInfo.nickName }}</em>
            <img src="@/assets/rightArrow.png" alt="" class="rightArrow2" />
          </div>
        </div>
        <div class="lineItem" @click="showSex = true">
          <div class="leftLineItem">性别</div>
          <div class="rightLineItem">
            <em v-if="resUserInfo.sex == 0">请设置性别</em>
            <em v-if="resUserInfo.sex == 1">男</em>
            <em v-if="resUserInfo.sex == 2">女</em>
            <img src="@/assets/rightArrow.png" alt="" class="rightArrow2" />
          </div>
        </div>
          <div class="lineItem" @click="jumpReciveAddrList">
            <div class="leftLineItem">收货信息</div>
            <div class="rightLineItem">
              <em v-if="resUserInfo.schoolName">{{ resUserInfo.schoolName||'请完善收货地址' }}</em>
              <img src="@/assets/rightArrow.png" alt="" class="rightArrow2" />
            </div>
          </div>
      </div>
    </div>
    <!-- 遮罩层-昵称 -->
    <div class="maskWrap" v-if="showNickName">
      <div class="maskBg" @click="showNickName = false"></div>
      <div class="chooseNumWrap">
        <div class="maskTitle">修改昵称</div>
        <div class="inputWrap">
          <van-field
            type="text"
            v-model="nickName"
            maxlength="8"
            placeholder="请输入昵称"
          />
        </div>
        <div class="check" style="display: none">
          <em class="nick">昵称已被占用</em>
          <em class="onlyOne">仅可修改一次</em>
        </div>
        <div class="sure" @click="getUpdateAppUser()">确定</div>
        <div class="sure dele" @click="showNickName = false">取消</div>
      </div>
    </div>
    <!-- 遮罩层-修改性别 -->
    <div class="maskWrap" v-if="showSex">
      <div class="maskBg" @click="showSex = false"></div>
      <div class="chooseNumWrap">
        <div class="maskTitle">修改性别</div>
        <div class="check check2">
          <div class="sexClass" :class="{ male: sex === 2 }" @click="sex = 2">
            <img src="@/assets/sex.png" alt="" class="seximg" />
            女
          </div>
          <div class="sexClass" :class="{ female: sex === 1 }" @click="sex = 1">
            <img src="@/assets/sex2.png" alt="" class="seximg" />
            男
          </div>
        </div>
        <div class="sure" @click="getUpdateAppUser()">确定</div>
        <div class="sure dele" @click="showSex = false">取消</div>
      </div>
    </div>
    <!-- <tabbar></tabbar> -->
  </div>
</template>

<script>
import axios from "axios"; // 引入axios
import { Field, Uploader ,ImagePreview} from "vant";
import { apiAppUserInfo, apiUpdateAppUser } from "@/request/api";
import tabbar from "../components/tabbar";
export default {
  name: "App",
  data() {
    return {
      showNickName: false,
      showSex: false,
      resUserInfo: "",
      fileList: [],
      headImage: "",
      nickName: "",
      sex: 0,
    };
  },
  mounted() {
    document.querySelector("body").setAttribute("style", "background-color:#F2F4FA");
    this.getAppUserInfo();
  },
  methods: {
    // 查看头像图片
    look(img){
    ImagePreview([img]);
    },
    //获取用户数据：
    getAppUserInfo() {
      apiAppUserInfo({
        id: -1,
      }).then((res) => {
        this.resUserInfo = res.data;
        console.log("getAppUserInfo", this.resUserInfo);
      });
    },
    //删除已经存在的头像：
    deleMyHead(){
      this.resUserInfo.headImage = null
    },
    //上传图片到服务器：
    afterRead(file) {
      console.log("file:", file);
      var serverSrc = this.$imgUrl;
      var form_data = new FormData();
      var that = this
      console.log("form_data", form_data);
      var files = file.file;
      form_data.append("file", files);
      axios({
        url: serverSrc + "/common/upload",
        data: form_data,
        contentType: false,
        processData: false,
        method: "post",
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }).then(function (res) {
        console.log("resImg:", res);
        that.headImage = res.data.data.fileName;
        that.getUpdateAppUser();
      });
    },
    //更新用户数据：
    getUpdateAppUser() {
      apiUpdateAppUser({
        headImage: this.headImage,
        nickName: this.nickName,
        sex: this.sex,
      }).then((res) => {
        console.log("更新用户信息", res);
        if (res.code == 200) {
          this.showNickName = false;
          this.showSex = false;
          this.getAppUserInfo();
        }
      });
    },
    handleFiles(files) {
      console.log("lk", files);
    },
    onConfirm() {
      this.$refs.item.toggle();
    },
    jumpBanner() {
      this.$router.push({
        path: "/pages/bannerInfo",
        query: {
          //传递参数
        },
      });
    },
    jumpReciveAddrList(){
      this.$router.push({
        path: "/pages/reciveAddrList",
        query: {
          //传递参数
          formId:5
        },
      });
    }
  },
  created() {},
  components: {
    tabbar,
    [Field.name]: Field,
    [Uploader.name]: Uploader,
    [ImagePreview.Component.name]: ImagePreview.Component,
  },
};
</script>

<style scoped>
.main {
}
.item {
  overflow: hidden;
  margin-bottom: 20px;
  width: calc(95% - 30px);
  margin: 20px auto;
  background-color: #fff;
  border-radius: 20px;
  padding: 15px;
}
.left {
  float: left;
  font-size: 13px;
  font-weight: 400;
  color: #181818;
  margin-top: 25px;
}
.right {
  float: right;
}
.headImgWrap {
  float: right;
  position: relative;
}
.deleImg {
  position: absolute;
  width: 15px;
  top: 5px;
  right: 0px;
}
.head {
  width: 65px;
  height: 65px;
  border-radius: 65px;
}
.rightArrow {
  width: 5px;
  height: 9px;
  margin-bottom: 25px;
  margin-left: 10px;
  float: right;
  margin-top: 27px;
}
/deep/.van-uploader__wrapper {
  float: right !important;
  width: 100px;
}
/deep/.van-uploader__upload {
  width: 65px;
  height: 65px;
}
/depp/.van-uploader__preview-image {
  border-radius: 80px;
}
.lineItem {
  overflow: hidden;
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 400;
  color: #181818;
  height: 50px;
  line-height: 50px;
}
.leftLineItem {
}
.rightLineItem {
}
.rightArrow2 {
  width: 5px;
  height: 9px;
  padding-left: 10px;
}
.maskWrap {
}
.maskBg {
  width: 100%;
  min-height: 100vh;
  opacity: 0.35;
  background-color: #000;
  position: fixed;
  top: 0px;
}
.chooseNumWrap {
  width: 80%;
  background-color: #fff;
  position: fixed;
  left: 10%;
  bottom: 35vh;
  border-radius: 20px;
}
.maskTitle {
  font-size: 14px;
  font-weight: 500;
  color: #181818;
  text-align: center;
  margin: 20px 0px;
}
.inputWrap {
  width: 90%;
  margin: 0 auto;
  height: 40px;
  /* background: #f4f8fb; */
  border-radius: 20px;
  margin-top: 15px;
  margin-bottom: 30px;
}
.van-cell {
  background-color: #f4f8fb;
  border-radius: 20px;
}
.check {
  width: 80%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  font-weight: 300;
}
.check2 {
  width: 50%;
  margin-bottom: 20px;
}
.sexClass {
  color: #181818;
  padding: 5px 15px;
  border-radius: 15px;
  font-weight: 600;
}
.male {
  border: 1px solid #f8a4a7;
}
.female {
  border: 1px solid#4d63ff;
}
.seximg {
  width: 12px;
  height: 12px;
  float: left;
  margin-right: 5px;
  margin-top: 1px;
}
.nick {
  color: #999999;
  display: block;
}
.onlyOne {
  display: block;
  color: #181818;
}
.sure {
  width: 115px;
  height: 34px;
  line-height: 34px;
  background: #181818;
  border-radius: 17px;
  text-align: center;
  font-size: 14px;
  font-weight: bold;
  color: #ffffff;
  margin: 0 auto;
  margin-bottom: 15px;
}
.dele {
  background-color: #fff;
  color: #999999;
}
</style>
